<?php

class Account extends AppModel {

    function isUnique($field, $value) {
        $fields[$this->name . '.' . $field] = $value;


        $this->recursive = -1;
        if ($this->hasAny($fields)) {
            $this->invalidate('unique_' . $field);
            return false;
        }
        else
            return true;
    }

    /**
     * Description of "assignPos" 
     * 
     * This function takes the modified arrays of users and insert each user in the database table 
     * account_has_users each one with his specified permission...
     *
     * @param array $mod
     * @param array $ed
     * @param array $jed
     *
     * @author karim kamel
     */
    function assignPos($mod, $ed, $accID) {
        $this->query("delete from accounts_users where accounts_users.account_id = " . $accID . " ");
        if ($mod[0] == "empty" && $ed[0] == "empty") {
            
        } else {
            for ($i = 0; $i < count($mod); $i++) {
                $email = $mod[$i];
                $this->query("insert into accounts_users(account_id,user_email,permission) values(" . $accID . ",'" . $email . "','moderator')");
            }
            for ($j = 0; $j < count($ed); $j++) {
                $email = $ed[$j];
                $this->query("insert into accounts_users(account_id,user_email,permission) values(" . $accID . ",'" . $email . "','editor')");
            }
        }
    }

    /**
     * Description of (getAccountNameByAccID)
     *
     * This function takes an account ID and returns its twitter name
     *
     * @param int $accID
     *
     * @author karim kamel
     */
    function getAccountNameByAccID($accID) {
        return $this->find('all', array('conditions' => array('id' => $accID), 'fields' => array('screen_name')));
    }

    /**
     * Gets the oauth token and oauth token secret from table tattlr_accounts
     * @param  integer $accID .
     * @return array 
     * author : "omar ruedy"
     */
    function getTwitterAccountKeys($accountID) {
        return $this->find('all', array('conditions' => array('id' => $accountID), 'fields' => array('access_token', 'access_secret')));
    }

    /*
     * Checks if account exists in the db,then checks if user has permission for this account on twitter or not(access not revoked).
     * @param  $accountID
     * @return bool
     * @author "Omar Ruedy"
     */

    function accountIsAuthorized($accountID) {

        $account = $this->getTwitterAccountKeys($accountID);

        if (!empty($account)) {
            $connection = new TwitterOAuth('JpsL9pQYCrNIXTFqWizM3Q', 'NetGak3BkvZq4JTucIbzH7549NeClMfl7ZBEclt2cSs', $account['0']['Account']['access_token'], $account['0']['Account']['access_secret']);
            $content = $connection->get('account/verify_credentials');
            if ($connection->http_code == 200) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /*
     * Requests a connection object on which twitter api calls can be called upon.
     * @param  $accountID
     * @return TwitterOAuth $connection
     * @author "Omar Ruedy"
     */

    function connectionRequest($accountID) {
        if ($this->accountIsAuthorized($accountID)) {
            $account = $this->getTwitterAccountKeys($accountID);

            $connection = new TwitterOAuth('JpsL9pQYCrNIXTFqWizM3Q', 'NetGak3BkvZq4JTucIbzH7549NeClMfl7ZBEclt2cSs', $account['0']['Account']['access_token'], $account['0']['Account']['access_secret']);
            $content = $connection->get('account/verify_credentials');
            return $connection;
        }
        else
            return FALSE;
    }

    function ddb() {
        $this->query("
          drop table if exists accounts_has_users ;
		  drop table if exists accounts_users ;
          drop table if exists invitations ;
          drop table if exists tweets_count ;
          drop table if exists followers_count ;
          drop table if exists retweets_count ;
          drop table if exists favourites_count ;
          drop table if exists future_tweets ;
          drop table if exists tweets ;
          drop table if exists users ;
          drop table if exists accounts ;
          
create table accounts(
screen_name varchar(30) not null unique,
id integer primary key,
access_token varchar(100),
access_secret varchar(100),
image varchar(200)
);

create table users (
email varchar(100) primary key not null,
first_name varchar(30),
last_name varchar(30),
password varchar(100) not null,
timezone varchar(3),
current_account integer,
foreign key (current_account) references accounts(id)
);

create table tweets(

id varchar(40) not null primary key,
tweet_text text not null,
account_id integer,
user_email varchar(100),
foreign key (user_email) references users(email)
);

create table future_tweets(
status varchar(10) default 'pending',
id integer primary key not null auto_increment,
tweet_text text,
account_id integer,
user_email varchar(100),
release_date datetime,
foreign key (account_id) references accounts(id),
foreign key (user_email) references users(email)
);

create table favourites_count(
account_id integer,
id integer auto_increment primary key,
count integer,
foreign key(account_id) references accounts(id)
);

create table retweets_count(
account_id integer,
id integer auto_increment primary key,
count integer,
foreign key(account_id) references accounts(id)
);

create table followers_count(
account_id integer,
id integer auto_increment primary key,
count integer,
foreign key(account_id) references accounts(id)
);

create table tweets_count(
account_id integer,
id integer auto_increment primary key,
count integer,
foreign key(account_id) references accounts(id)
);

create table invitations(
account_id integer,
user_email varchar(100),
primary key(account_id,user_email),
permission varchar(10),
foreign key (user_email) references users(email),
foreign key (account_id) references accounts(id)
);

create table accounts_users(
account_id integer,
user_email varchar(100),
primary key(account_id,user_email),
permission varchar(10),
foreign key (user_email) references users(email),
foreign key (account_id) references accounts(id)
);
Alter Table favourites_count Add query_time TIMESTAMP;
Alter Table retweets_count Add query_time TIMESTAMP;
INSERT INTO `tweetar`.`users` (`email`, `password`) VALUES ('admin@gmail.com', '48bb6eee0e5393e339f8dc2179a7e79f');
          
");
    }

}

?>
